package com.ewzj.bigdata.app.extensions

import android.app.AlertDialog
import android.os.Build
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.webkit.*
import android.widget.TextView
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.mvp.ui.view.ObservableWebView
import com.ewzj.bigdata.mvp.ui.view.pathanimlib.PathAnimView
import com.github.lzyzsd.jsbridge.BridgeWebView
import com.github.lzyzsd.jsbridge.DefaultHandler
import com.jess.arms.utils.DataHelper
import java.util.HashMap

/**
 * author:zhangjing
 * 作用:
 * return:
 */
fun TextView.setPageTurningPointLocation(bottomMargin:Int):Unit{

}
/*
    获取偏移量
 */
fun RecyclerView.getScollYDistance(): Int {
    val layoutManager = this.getLayoutManager() as LinearLayoutManager
    val position = layoutManager.findFirstVisibleItemPosition()
    if (position != 0) {
        return 300.dpToPx(this.context)
    } else {
        val firstVisiableChildView = layoutManager.findViewByPosition(position)
        val itemHeight = firstVisiableChildView.height
        return position * itemHeight - firstVisiableChildView.top
    }

}

/*
* 初始化刷新的颜色
*
* */

fun SwipeRefreshLayout.initcolor():Unit{
        this.setColorSchemeResources(R.color.maingreen, R.color.color_blue)
}

fun BridgeWebView.loadMiwoUrl(url:String):Unit{
    var apiToken=DataHelper.getStringSF(this.context,AppKey.API_TOKEN)
    if (!apiToken.isNullOrEmpty()) {
        val extraHeaders = HashMap<String, String>()
        extraHeaders.put("token", apiToken)
        this.loadUrl(url, extraHeaders)
    } else {
        this.loadUrl(url)
    }
}

fun BridgeWebView.goMiwoUrl(url:String):Unit{
    var apiToken=DataHelper.getStringSF(this.context,AppKey.API_TOKEN)
    if (!apiToken.isNullOrEmpty()) {
        val extraHeaders = HashMap<String, String>()
        extraHeaders.put("token", apiToken)
        this.loadUrl(url, extraHeaders)
    } else {
        this.loadUrl(url)
    }
}
/*
* 初始化webview
* */

fun BridgeWebView.initwebview(rlloading:View){
    var websetting=this.settings
    websetting.javaScriptEnabled=true
    websetting.defaultTextEncodingName="utf-8"
    websetting.setRenderPriority(WebSettings.RenderPriority.HIGH)
    websetting.allowFileAccessFromFileURLs=true
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        websetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)
    }
    websetting.setBlockNetworkImage(true)
    websetting.setDomStorageEnabled(true)
    this.setDefaultHandler(DefaultHandler())
    this.setWebChromeClient(object : WebChromeClient() {
        override fun onProgressChanged(view: WebView, newProgress: Int) {
            if (newProgress == 100) {
                websetting.setBlockNetworkImage(false)
                rlloading.setVisibility(View.GONE)
                (rlloading.findViewById(R.id.pathAnimView1) as PathAnimView).stopAnim()
            }
            super.onProgressChanged(view, newProgress)
        }

        override fun onJsAlert(view: WebView, url: String, message: String, result: JsResult): Boolean {
            val b = AlertDialog.Builder(context)
            b.setTitle("Alert")
            b.setMessage(message)
            b.setPositiveButton(android.R.string.ok) { dialog, which -> result.confirm() }
            b.setCancelable(false)
            b.create().show()
            return true
        }

        override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean {
            Log.d("test", consoleMessage.message() + " -- From line "
                    + consoleMessage.lineNumber() + " of "
                    + consoleMessage.sourceId())
            return true
        }
    })

}